package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.ManagerClass;
import com.markerhub.entity.ManagerStudent;
import com.markerhub.mapper.ManagerClassMapper;
import com.markerhub.mapper.ManagerStudentMapper;
import com.markerhub.service.ManagerClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerClassServiceImpl extends ServiceImpl<ManagerClassMapper, ManagerClass> implements ManagerClassService {
@Autowired
ManagerClassMapper classMapper;
@Autowired
    ManagerStudentMapper studentMapper;
    @Override
    public ManagerClass getByClassName(String className) {
        return this.getOne(new LambdaQueryWrapper<ManagerClass>().eq(ManagerClass::getName, className));
    }
    @Override
    public IPage<ManagerClass> getClassesByActivityName(String activityName, int currentPage, int pageSize) {
        Page<ManagerClass> page = new Page<>(currentPage, pageSize);

        // 获取教学班数量
        Integer count = classMapper.countClassesByActivityName(activityName);

        // 分页查询教学班列表
        IPage<ManagerClass> classesPage = classMapper.getClassesByActivityName(page, activityName);

        return classesPage;
    }

    @Override
    public IPage getClasses(String activityName, String className, String classDesc, String teacherName, int currentPage, int pageSize) {
        Page<ManagerClass> page = new Page<>(currentPage, pageSize);

        Integer countClasses = classMapper.countClasses(activityName, className, classDesc, teacherName);
        IPage<ManagerClass> classIPage = classMapper.getClasses(page, activityName, className, classDesc, teacherName);
        return classIPage;
    }

    @Override
    public IPage<ManagerClass> getTeacherClasses(Page<ManagerClass> page, Long teacherId, String className) {
        return baseMapper.selectPage(page, new LambdaQueryWrapper<ManagerClass>()
                .eq(ManagerClass::getTeacherId, teacherId)
                .like(StringUtils.isNotBlank(className), ManagerClass::getName, className)
                .orderByDesc(ManagerClass::getCreatedTime));
    }

    @Override
    public List<ManagerStudent> getClassStudents(Integer classId) {
        return studentMapper.selectClassStudents(classId);
    }


}
